<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/08/16 11:45:02 $)
// $RCSfile: impex.users.php,v $ : $Revision: 1.1 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/tickets/tickets.php");

/**
* Return the Total Pages for this Plugin
*/
function getPluginTotalPages2()
{
	global $impexDB;

	$_total = $impexDB->queryFetch("SELECT COUNT(*) AS totalemails FROM `address`;");
	$finaltotal = intval($_total["totalemails"]);

	$totalpages = ceil($finaltotal/IMPEX_PASS);

	return $totalpages;
}

/**
* Run the specific page for plugin
*/
function runPluginPage2($page)
{
	global $dbCore, $impexDB, $_IMPEXHASH, $_SWIFT;

	if ($page == 1)
	{
		unset($_POST["ticketstartat"]);
	}

	if (!is_array($_IMPEXHASH["useremails"]))
	{
		$_IMPEXHASH["useremails"] = array();
	}

	if (empty($_POST["ticketstartat"]))
	{
		$_startat = 1;
	} else {
		$_startat = intval($_POST["ticketstartat"]);
	}

	$staffemaillist = $ticketemaillist = $userpointer = $finalticketemaillist = $indexpointer = $emaillist = $finalemaillist = $currentemaillist = $currentpointer = $addedlist = array();
	// Get all Staff Emails
	$dbCore->query("SELECT `email` FROM `". TABLE_PREFIX ."staff` ORDER BY `staffid` ASC;");
	while ($dbCore->nextRecord())
	{
		if (!in_array($dbCore->Record["email"], $staffemaillist))
		{
			$staffemaillist[] = $dbCore->Record["email"];
		}
	}

	$lastticketid = 1; $index = 1;
	$impexDB->query("SELECT * FROM `address` WHERE `address_id` >= '". intval($_startat) ."' ORDER BY `address_id` ASC LIMIT ". intval(IMPEX_PASS) .";");
	while ($impexDB->nextRecord())
	{
		if (!in_array($impexDB->Record["address_address"], $staffemaillist) && isValidEmail($impexDB->Record["address_address"]))
		{
			$ticketemaillist[$index]["address_id"] = $impexDB->Record["address_id"];
			$ticketemaillist[$index]["email"] = $impexDB->Record["address_address"];
			$index++;
		}
		$lastticketid = $impexDB->Record["address_id"];
	}

	foreach ($ticketemaillist as $key=>$val)
	{
		$userid = _impexInsertUser("2", $val["email"], $val["email"], substr(buildHash(),0,6), "1", "1");
		$_IMPEXHASH["userpointer"][$val["address_id"]] = $userid;
		$currentpointer[$val["email"]] = $userid;

		displayStatus("Imported User: ".htmlspecialchars($val["email"]));
	}

	// Now.. we need to update the tickets table with user ids
	foreach ($currentpointer as $key=>$val)
	{
		_impexTagUserID($key, $val);

		displayStatus("Tagging Tickets For: ".htmlspecialchars($key)." (ID: ". intval($val) .")");
	}

	$_SWIFT["ticketstartat"] = $lastticketid+1;
}

?>